﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace LiveWriterBTFL
{
    public class PluginHelper
    {
        public const string FB = "http://facebook.com/";
        public const string LI = "http://www.linkedin.com/in/";
        public const string Tw = "https://twitter.com/";
        private string _example = "";

        public string Example { get { return _example; } set { _example = value; } }

        public static void OpenSearch(string address) 
        {
            Process.Start(address);
        }

        public string CreateBTFString(string TwitterUserName, string FacebookUserName, string LinkedInUserName, string BlogAddress)
        {
            string result = "";
            string startString = "(";
            string endString = ")";
            string Separator = "|";
            bool isfilled = false;

            string FinalText = startString;

            if (TwitterUserName != "")
            {
                isfilled = true;
                FinalText += "<a href=\"" + Tw + TwitterUserName + "\" target=\"_blank\" alt=\"The user's twitter profile\">T</a>";
                Example += "T@";
            }
            if (BlogAddress != "")
            {
                isfilled = true;
                FinalText += "<a href=\"" + BlogAddress + "\" target=\"_blank\" alt=\"The user's blog\">B</a>";
                Example += "B@";
            }
            if (FacebookUserName != "")
            {
                isfilled = true;
                FinalText += "<a href=\"" + FB + FacebookUserName + "\" target=\"_blank\" alt=\"The user's Facebook profile\">F</a>";
                Example += "F@";
            }
            if (LinkedInUserName != "")
            {
                isfilled = true;
                FinalText += "<a href=\"" + LI + LinkedInUserName + "\" target=\"_blank\" alt=\"The user's LinkedIn profile\">L</a>";
                Example += "L@";
            }
            if(!isfilled)
            {
                throw new Exception("You need at least one provider");

            }
            FinalText = FinalText.Replace("</a><a", "</a>" + Separator + "<a") + endString;
            result = FinalText;

            Example = startString + Example.Replace("@", "|").Substring(0, Example.Length - 1) + endString;
            return result;

        }

        internal string CreateBTFEditorString(string TwitterUserName, string FacebookUserName, string LinkedInUserName, string BlogAddress)
        {
            
            string FinalText = "";

            if (TwitterUserName != "")
            {
                FinalText += "T=" + TwitterUserName + ";" ;
            }
            if (BlogAddress != "")
            {
                FinalText += "B=" + BlogAddress + ";";
                
            }
            if (FacebookUserName != "")
            {
                FinalText += "F=" + FacebookUserName + ";";
                
            }
            if (LinkedInUserName != "")
            {
                FinalText += "L=" + LinkedInUserName + ";";
            }
            return FinalText;
        }
        internal string FindEditorStringProperty(string socialProvider, string StringToSplit)
        {
            string result = "";
            try
            {
                string[] Splitted = StringToSplit.Split(';');
                for(int i = 0; i < Splitted.Length;i++)
                {
                    if(Splitted[i].Split('=')[0] == socialProvider)
                    {
                        //bingo
                        result = Splitted[i].Split('=')[1];
                        break;
                    }
                          
                }
            }
            catch
            {
                return "";
            }
            return result;
             
        }
    }
}
